package com.ruoyi.tongyan.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.tongyan.domain.TProjectContract;
import com.ruoyi.tongyan.mapper.TProjectContractMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tongyan.mapper.TContractMapper;
import com.ruoyi.tongyan.domain.TContract;
import com.ruoyi.tongyan.service.ITContractService;

/**
 * 项目合同Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class TContractServiceImpl implements ITContractService
{
    @Autowired
    private TContractMapper tContractMapper;
    @Autowired
    private TProjectContractMapper tProjectContractMapper;

    /**
     * 查询项目合同
     *
     * @param contractId 项目合同主键
     * @return 项目合同
     */
    @Override
    public TContract selectTContractByContractId(Long contractId)
    {
        return tContractMapper.selectTContractByContractId(contractId);
    }

    /**
     * 查询项目合同列表
     *
     * @param tContract 项目合同
     * @return 项目合同
     */
    @Override
    public List<TContract> selectTContractList(TContract tContract)
    {
        return tContractMapper.selectTContractList(tContract);
    }

    /**
     * 新增项目合同
     *
     * @param tContract 项目合同
     * @return 结果
     */
    @Override
    public int insertTContract(TContract tContract,Long projectId)
    {
        //新增合同
        tContractMapper.insertTContract(tContract);
        //新增项目合同关联记录
        TProjectContract tProjectContract = new TProjectContract();
        tProjectContract.setContractId(tContract.getContractId());
        tProjectContract.setProjectId(projectId);
        return tProjectContractMapper.insertTProjectContract(tProjectContract);
    }

    /**
     * 修改项目合同
     *
     * @param tContract 项目合同
     * @return 结果
     */
    @Override
    public int updateTContract(TContract tContract)
    {
        return tContractMapper.updateTContract(tContract);
    }

    /**
     * 批量删除项目合同
     *
     * @param ids 需要删除的项目合同主键
     * @return 结果
     */
    @Override
    public int deleteTContractByContractIds(Long[] ids)
    {
        List<Long> contractIds = new ArrayList<>();
        // 遍历所有id，获取对应的contractId
        for (Long id : ids) {
            TProjectContract contract = tProjectContractMapper.selectTProjectContractById(id);
            if (contract != null) {
                contractIds.add(contract.getContractId());
            }
        }
        if (contractIds.isEmpty()) {
            return 0;
        }
        // 将List<Long>转换为Long[]
        Long[] contractIdsArray = contractIds.toArray(new Long[0]);
        //删除操作，删除关联表中记录和相关合同记录
        tProjectContractMapper.deleteTProjectContractByIds(ids);
        return tContractMapper.deleteTContractByContractIds(contractIdsArray);
    }

    /**
     * 删除项目合同信息
     *
     * @param contractId 项目合同主键
     * @return 结果
     */
    @Override
    public int deleteTContractByContractId(Long contractId)
    {
        return tContractMapper.deleteTContractByContractId(contractId);
    }
}
